<?php

/**
 * @package Joostina BOSS
 * @copyright Авторские права (C) 2008-2010 Joostina team. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * Joostina BOSS - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Joostina BOSS основан на разработках Jdirectory от Thomas Papin
 */
defined('_VALID_MOS') or die();
require_once( JPATH_BASE . DS . 'components' . DS . 'com_boss' . DS . 'boss.tools.php' );
require_once( JPATH_BASE . DS . 'components' . DS . 'com_boss' . DS . 'lang' . DS . 'russian.php' );
$act = mosGetParam($_REQUEST, 'act', '');
$task = mosGetParam($_REQUEST, 'task', '');
$directory = mosGetParam($_REQUEST, 'directory', 0);

switch ($act) {
    case "fields":
        $table = "#__boss_" . $directory . "_fields";
        $where_field = 'fieldid';
        $id = mosGetParam($_REQUEST, 'tid', '');

        switch ($task) {
            case "publish":
                $field = 'published';
                changeState($table, $field, $id, $where_field);
                break;
            case "required":
                $field = 'required';
                changeState($table, $field, $id, $where_field);
                break;
            default :
                break;
        }

        break;
    case "groups":

        switch ($task) {
            case "publish":
                $table = "#__boss_" . $directory . "_groups";
                $field = 'published';
                $where_field = 'id';
                $id = mosGetParam($_REQUEST, 'tid', '');
                changeState($table, $field, $id, $where_field);
                break;
            default :
                break;
        }

        break;
    case "categories":

        switch ($task) {
            case "publish":
                $table = "#__boss_" . $directory . "_categories";
                $field = 'published';
                $where_field = 'id';
                $id = mosGetParam($_REQUEST, 'tid', '');
                changeState($table, $field, $id, $where_field);
                break;
            default :
                break;
        }

        break;
    case "contents":

        switch ($task) {
            case "publish":
                $table = "#__boss_" . $directory . "_contents";
                $field = 'published';
                $where_field = 'id';
                $id = mosGetParam($_REQUEST, 'tid', '');
                changeState($table, $field, $id, $where_field);
                break;
            default :
                break;
        }

        break;

    case "upload_image":
        upload_image($directory);
        break;

    case "delete_pack":
        delete_pack($directory);
        break;

    case "plugins":
         switch ($task) {
            case "run_plugins_func":
                $class      = mosGetParam($_REQUEST, 'class', '');
                $function   = mosGetParam($_REQUEST, 'function', '');
                $path       = JPATH_BASE.DS.'images'.DS.'boss'.DS.$directory.DS.'plugins'.DS.'fields'.DS.$class.'/plugin.php';
                if(is_file($path)){
                    require_once($path);
                }
                else{
                    break;
                }
                $class = new $class;
                $class->$function();
                unset($directory, $class, $function, $path);
                break;
            default :
                break;
        }
        break;

    default :
        break;
}


function changeState($table, $field, $id, $where_field) {
    if (!empty($id)) {
        $database = database::getInstance();
        $object = null;
        //вычисляем состояние поля
        $database->setQuery("SELECT `$field` FROM $table WHERE `$where_field` = $id LIMIT 1");
        $database->query();
        $state = $database->loadResult();
        //меняем состояние на противоположное
        if ($state == 1)
            $state_new = 0;
        else
            $state_new = 1;
        //обновляем состояние
        $database->setQuery("UPDATE $table SET `$field` = '$state_new' WHERE `$where_field` = $id ");
        $database->query();
        if ($database->getErrorNum())
            $ret = 'error.png';
        else {
            if (strpos($table, "_contents") === false)  {
                $ret = ($state_new == 1) ? 'tick.png' : 'publish_x.png';
            }
            else{
                $date = date('Y-m-d');
                $q = "SELECT `published`, `date_publish`, `date_unpublish` FROM $table WHERE `id` = $id LIMIT 1";
                $database->setQuery($q); $ret = $q;
                $database->query();
                $database->loadObject($object);
                if ($object->published == 0){
                    $ret = 'publish_x.png';
                }
                elseif($object->published == 1 && ($object->date_publish > $date && $object->date_publish != '0000-00-00')){
                   $ret = 'publish_y.png';
                }
                elseif($object->published == 1 && ($object->date_unpublish < $date && $object->date_unpublish != '0000-00-00') ){
                   $ret = 'publish_r.png';
                }
                else{
                   $ret = 'tick.png';
                }
            }
        }


        echo $ret;
    }
}

function upload_image($directory) {
    $file = JPATH_BASE."/images/boss/$directory/fields/" . basename($_FILES['uploadfile']['name']);

    if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
        echo "success";
    } else {
        echo "error";
    }
}

function delete_pack($directory) {
    $pack = mosGetParam($_REQUEST, 'pack', '');
    $file = JPATH_BASE."/images/boss/$directory/$pack.zip";
    if(is_file($file))
        unlink($file);
    if(is_file($file))
        echo 'no';
    else
        echo 'yes';
}
?>
